<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="renderer" content="webkit">
    <title></title>
    <link rel="stylesheet" href="/static/css/pintuer.css">
    <link rel="stylesheet" href="/static/css/admin.css">
    <script src="/static/js/jquery.js"></script>
    <script src="/static/js/pintuer.js"></script>
</head>
<body>
<div class="panel admin-panel">
    <div class="panel-head"><strong><span class="icon-key"></span> 修改会员密码</strong></div>
    <div class="body-content">
        <form method="post" class="form-x" action="">
            <div class="form-group">
                <div class="label">
                    <label for="uname">管理员帐号：</label>
                </div>
                <div class="field">
                    <label style="line-height:33px;">
                        <span id="uname">admin</span>
                    </label>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label for="mpass">原始密码：</label>
                </div>
                <div class="field">
                    <input type="password" class="input w50" id="mpass" name="mpass" size="50" placeholder="请输入原始密码"
                           data-validate="required:请输入原始密码"/>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label for="newpass">新密码：</label>
                </div>
                <div class="field">
                    <input type="password" class="input w50" id="newpass" name="newpass" size="50" placeholder="请输入新密码"
                           data-validate="required:请输入新密码,length#>=5:新密码不能小于5位"/>
                </div>
            </div>
            <div class="form-group">
                <div class="label">
                    <label for="renewpass">确认新密码：</label>
                </div>
                <div class="field">
                    <input type="password" class="input w50" id="renewpass" name="renewpass" size="50"
                           placeholder="请再次输入新密码" data-validate="required:请再次输入新密码,repeat#newpass:两次输入的密码不一致"/>
                </div>
            </div>

            <div class="form-group">
                <div class="label">
                    <label></label>
                </div>
                <div class="field">
                    <button class="button bg-main icon-check-square-o" type="submit"> 提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    $(document).ready(function () {
        // 获取管理员账号从sessionStorage中并显示在id为uname的span标签中
        var adminAccount = sessionStorage.getItem('username');
        console.log(adminAccount);
        $("#uname").text(adminAccount);

        // 表单提交时触发事件
        $("form").submit(function (e) {
            e.preventDefault(); // 阻止表单默认提交行为

            // 获取输入的原始密码、新密码和确认新密码
            var originalPassword = $("#mpass").val();
            var newPassword = $("#newpass").val();
            var confirmPassword = $("#renewpass").val();

            // 使用AJAX请求检查原始密码是否正确
            $.ajax({
                url: 'UserServlet?methodName=getUser', // 替换成验证密码的后端接口
                type: 'get',
                data: 'name=' + adminAccount,
                dataType: 'json',
                success: function (response) {

                    if (response.data.pwd === originalPassword) {
                        // 原始密码正确，检查新密码和确认新密码是否一致
                        if (newPassword === confirmPassword) {
                            // 使用AJAX请求修改密码
                            $.ajax({
                                url: 'UserServlet?methodName=changePwd', // 替换成修改密码的后端接口
                                type: 'get',
                                data: 'name=' + adminAccount + '&newPwd=' + confirmPassword,
                                dataType: 'json',
                                success: function (result) {
                                    if (result.code === 1) {
                                        alert('密码修改成功！');
                                    } else {
                                        alert('密码修改失败，请稍后再试。');
                                    }
                                },
                                error: function () {
                                    alert('密码修改请求失败，请稍后再试。');
                                }
                            });
                        } else {
                            alert('新密码和确认新密码不一致，请重新输入。');
                        }
                    } else {
                        alert('原始密码不正确，请重新输入。');
                        $("#mpass").val("");
                    }
                },
                error: function () {
                    alert('密码验证请求失败，请稍后再试。');
                }
            });
        });
    });

</script>
</html>
